package com.jeecms.wicool.dao.cms.impl;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.cms.CmsInfoDao;
import com.jeecms.wicool.entity.cms.CmsInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CmsInfoDaoImpl extends HibernateBaseDao<CmsInfo, Integer> implements
        CmsInfoDao {

    @Override
    protected Class<CmsInfo> getEntityClass() {
        // TODO Auto-generated method stub
        return CmsInfo.class;
    }

    @Override
    public Pagination getPage(String title,
                              int pageNo, int pageSize, CmsUser user) {
        Finder f = Finder.create("select bean from CmsInfo bean where 1=1 ");
        if (!StringUtils.isBlank(title)) {
            f.append(" and bean.title = :title");
            f.setParam("title", title);
        }
        if (user.getShopId() != null) {
            f.append(" and bean.shop.id = :shopId");
            f.setParam("shopId", user.getShopId());
        }
        f.append(" order by bean.orderNum desc");
        return find(f, pageNo, pageSize);
    }

    @Override
    public Pagination getPage(String title, Short publishType, Integer pageNo, Integer pageSize) {
        Finder f = Finder.create("select bean from CmsInfo bean where 1=1 ");
        if (!StringUtils.isBlank(title)) {
            f.append(" and bean.title = :title");
            f.setParam("title", title);
        }

        if (publishType != null) {
            f.append(" and bean.isIndex=:isIndex");
            f.setParam("isIndex", publishType);
        }
        f.append(" order by bean.orderNum desc");
        return find(f, pageNo, pageSize);
    }

    @Override
    public CmsInfo findById(Integer id) {
        CmsInfo entity = get(id);
        return entity;
    }

    @Override
    public CmsInfo save(CmsInfo bean) {
        getSession().save(bean);
        return bean;
    }

    @Override
    public CmsInfo deleteById(Integer id) {
        CmsInfo entity = super.get(id);
        if (entity != null) {
            getSession().delete(entity);
        }
        return entity;
    }

    @Override
    public boolean isHave(CmsInfo bean) {
        Finder f = Finder
                .create("select bean from CmsInfo bean where 1 = 1 ");
        if (!StringUtils.isBlank(bean.getCmsPath())) {
            f.append(" and bean.cmsPath = :cmsPath");
            f.setParam("cmsPath", bean.getCmsPath());
        }
        if (bean.getShop().getId() != null) {
            f.append(" and bean.shop.id = :shopId");
            f.setParam("shopId", bean.getShop().getId());
        }
        if (bean.getIsIndex() != null) {
            f.append(" and bean.isIndex=:isIndex ");
            f.setParam("isIndex", bean.getIsIndex());
        }
        List<CmsInfo> list = find(f);
        if (list == null || list.size() <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public CmsInfo findByCmsInfo(CmsInfo bean) {
        Finder f = Finder
                .create("select bean from CmsInfo bean where 1 = 1 ");
        if (bean.getShop().getId() != null) {
            f.append(" and bean.shop.id = :shopId");
            f.setParam("shopId", bean.getShop().getId());
        }
        if (!StringUtils.isBlank(bean.getCmsPath())) {
            f.append(" and bean.cmsPath = :cmsPath");
            f.setParam("cmsPath", bean.getCmsPath());
        }
        List<CmsInfo> list = find(f);
        if (list != null) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<CmsInfo> getAll(CmsUser user) {
        Finder f = Finder
                .create("select bean from CmsInfo bean where 1 = 1 ");
        if (user.getShopId() != null) {
            f.append(" and bean.shop.id = :shopId");
            f.setParam("shopId", user.getShopId());
        }
        f.append(" order by bean.id desc");
        return find(f);
    }

    @Override
    public CmsInfo findByContentId(Integer contentId) {
        Finder f = Finder
                .create("select bean from CmsInfo bean where 1 = 1 ");
        if (contentId != null) {
            f.append(" and bean.contentId = :contentId");
            f.setParam("contentId", contentId);
        }
        List<CmsInfo> list = find(f);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }
}